'use strict';
/**
 * @name 莫凡服务端 业务层
 * @version 0.0.1
 * @author ljr
 * @Time 2020年10月21日11:06:27
 */
const Service = require('egg').Service;

const operation_log = '[morefun:操作日志]';

class templateService extends Service {
  async insert(data) {
    const {
      app,
      ctx
    } = this;
    try {
      let { pre_url, pre_img, template_version, template_id, template_name } = data
      let params = {
        pre_url, pre_img, template_version, template_id,template_name,
        gmt_create: new Date(),
        gmt_modified: new Date(),
      };
      let isHaveSql = `SELECT count(0) as count FROM template where template_id='${template_id}'`
      let isHave = await app.mysql.query(isHaveSql, '');
      if (!isHave[0].count) {
        //添加用户记录template
        let result = await app.mysql.insert('template', params);
        if (result.insertId != 0) {
          return { isOk: 1, data: { id: result.insertId, ...params } };
        } else {
          return { isOk: 0, msg: '添加失败，请重试' }
        }

      } else {
        return { isOk: 0, msg: '该模板已存在' }
      }
    } catch (err) {
      return { isOk: 0, msg: err }
    }

  }


  async findAll(data) {
    const {
      app,
      ctx
    } = this;
    try {
      let { curPage = 1, page_size = 10 } = data;

      let count = await app.mysql.query(`SELECT count(0) as count FROM template`, '');
      let templateSql = `SELECT id, id as t_id ,template_id, template_version,template_name, pre_img,pre_url FROM template limit ${(curPage - 1) * page_size},${page_size}`;
      let templateLIst = await app.mysql.query(templateSql, '');

      return { isOk: 1, data: { templateLIst, curPage, page_size, pageTotal: count[0].count } };
    } catch (err) {
      return { isOk: 0, msg: err }
    }
  }

  async findOne(data) {
    const {
      app,
      ctx
    } = this;
    let {  t_id } = data
    let templateSql = `SELECT template_id, template_version FROM template where id='${t_id}'`
    let template = await app.mysql.query(templateSql, '');

    if(template.length && template[0]) {
        return { isOk: 1, data: {template:template[0]} };
    } else {
      return { isOk: 0, msg: '没有此模板'};
    }
  }





  async update(data) {
    const {
      app,
      ctx
    } = this;
    let { id, pre_url, pre_img, template_version, template_id ,template_name} = data

    try {
      let isHaveSql = `SELECT count(0) as count FROM template where id='${id}'`
      let isHave = await app.mysql.query(isHaveSql, '');
      if (isHave[0].count) {

        let updateSql = `update template set pre_url='${pre_url}',pre_img='${pre_img}',template_name='${template_name}',template_version='${template_version}',template_id='${template_id}',gmt_modified=now() where id= '${id}'`
        let result = await app.mysql.query(updateSql, '');
        if (result.affectedRows != 0) {
          return { isOk: 1, data: result };
        } else {
          return { isOk: 0, msg: '更新失败，请重试' }
        }

      } else {
        return { isOk: 0, msg: '模板不存在' }
      }
    } catch (err) {
      return { isOk: 0, msg: err }
    }

  }

  async delete(data) {
    const {
      app,
      ctx
    } = this;
    try {
      let { id } = data
      let isHaveSql = `SELECT count(0) as count FROM template where id='${id}'`
      let isHave = await app.mysql.query(isHaveSql, '');
      if (isHave[0].count) {
        let deleteSql = `delete FROM template where id= '${id}'`
        let result = await app.mysql.query(deleteSql, '');
        if (result.affectedRows != 0) {
          return { isOk: 1, msg: '删除成功' };
        } else {
          return { isOk: 0, msg: '更新失败，请重试' }
        }

      } else {
        return { isOk: 0, msg: '模板不存在' }
      }
    } catch (err) {
      return { isOk: 0, msg: err }
    }
  }


  async testPost(user_id) {
    const {
      app,
    } = this;
    const result = {};
    result.user_id = user_id;

    app.logger.info(`${operation_log}删除了id为${user_id}的用户`);

    return result;
  }
}

module.exports = templateService;